#!/usr/bin  python
# _*_ coding:utf-8 _*_
"""
----------------------------------
    File Name: content sharing
    Description:
        Author: lhw
        Date: 2020/6/4
-----------------------------------
"""
from rest_framework.views import APIView
from EducationContent import models
from rest_framework.response import Response
from extensions.auth import JwtAuthorizationAuthentication
from django.db.models import Min, Max
from Common.logger import app_log
from Common.response import resp


class ContentCopy(APIView):

    # authentication_classes = [JwtAuthorizationAuthentication, ]

    def post(self, request, *args, **kwargs):
        try:
            content_id = request.data.get("content_id", "")
            category_id = request.data.get("category_id", "")
            hospital_id = request.data.get("hospital_id", "")
            single_content = models.EducationFeeds.objects.filter(id=content_id).first()
            data = single_content.educationFeeds_details_to_json()
            data.pop('id')
            data['category_id'] = category_id
            data['hospital_id'] = hospital_id
            current_max_order = models.EducationFeeds.objects.filter(
                hospital_id=hospital_id,
                category_id=category_id
            ).all().aggregate(Max('content_order'))['content_order__max']
            if current_max_order is None:
                current_max_order = 0
            content_order = current_max_order + 1
            data['content_order'] = content_order
            models.EducationFeeds.objects.create(**data)
            return Response({
                'error_code': 0,
                'message': "Success"
            })
        except Exception as e:
            app_log.exception(e)
            return resp(-1, e.args)
